package cn.dakaqi.web.groupRecruit;

import cn.dakaqi.entities.GroupRecruit;
import cn.dakaqi.group.recruit.services.GroupRecruitService;
import cn.dakaqi.group.services.GroupService;
import cn.dakaqi.member.service.VolunteerService;
import cn.dakaqi.web.BaseController;
import cn.dakaqi.web.cache.PageCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by chunyang on 2016/5/9.
 */
@Controller
@RequestMapping(value = "/groupRecruit")
public class GroupRecruitController extends BaseController
{
    @Autowired
    VolunteerService volunteerService;
    @Autowired
    GroupService groupService;
    @Autowired
    GroupRecruitService groupRecruitService;
    @Autowired
    PageCache pageCache;

    @Override
    protected ModelAndView initModelAndView(String groupCode,String view,HttpServletRequest request)
    {
        ModelMap modelMap = super.initData(groupCode,request);
        return new ModelAndView(view,modelMap);
    }
    @Override
    protected void clearModelAndView(String groupCode,HttpServletRequest request)
    {

    }
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView index(@RequestParam("groupCode") String groupCode,HttpServletRequest request)
    {
        String view = "groupRecruit/index";
        String key = groupCode+"/"+view;

        if(pageCache.exists(key))
        {
            modelAndView = pageCache.getModelAndView(key);
        }
        else
        {
            modelAndView = initModelAndView(groupCode, view,request);
            List<GroupRecruit> groupRecruits = groupRecruitService.findByGroupCode(groupCode);
            modelAndView.addObject("groupRecruits", groupRecruits);
            pageCache.setModelAndView(key,modelAndView);
        }

        return modelAndView;
    }

    @RequestMapping(value = "/add/{groupCode}", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("groupCode") String groupCode,HttpServletRequest request)
    {
        String view = "groupRecruit/create";
        String key = groupCode+"/"+view;
        if(pageCache.exists(key))
        {
            modelAndView = pageCache.getModelAndView(key);
        }
        else
        {
            modelAndView = initModelAndView(groupCode, view,request);
            List<GroupRecruit> groupRecruits = groupRecruitService.findByGroupCode(groupCode);
            modelAndView.addObject("groupRecruits", groupRecruits);
            pageCache.setModelAndView(key,modelAndView);
        }
        return modelAndView;
    }
    @RequestMapping(value = "/question/{groupCode}", method = RequestMethod.GET)
    public ModelAndView addQuestion(@PathVariable("groupCode") String groupCode,HttpServletRequest request)
    {
        String view = "groupRecruit/addquestion";
        String key = groupCode+"/"+view;
        if(pageCache.exists(key))
        {
            modelAndView = pageCache.getModelAndView(key);
        }
        else
        {
            modelAndView = initModelAndView(groupCode, view,request);
            List<GroupRecruit> groupRecruits = groupRecruitService.findByGroupCode(groupCode);
            modelAndView.addObject("groupRecruits", groupRecruits);
            pageCache.setModelAndView(key,modelAndView);
        }
        return modelAndView;
    }
}
